package com.oc.claimobs.bb.dao;

import java.util.Vector;

import com.oc.claimobs.data.Diccionario;
import com.oc.claimobs.data.DiccionarioCalles;

public class DiccionarioDao {
	
	private static DiccionarioDao instance = new DiccionarioDao();

	public static DiccionarioDao getInstance() {
		return instance;
	}

	private String[] dictionary = (new DiccionarioCalles()).getContent();
	private boolean orderedData = true;
	
	private int lenght = dictionary.length;
	
	public String[] startsWith(String prefix, int caseOptions) {
		Vector v = new Vector();
		String cstr = prefix;
		boolean blockfound = false; 
		for(int i = 0 ; i < lenght; i++){
			String s = dictionary[i];
			String g = s;
			if(caseOptions == Diccionario.CASE_INSENSITIVE){
				g = s.toLowerCase();
				cstr = prefix.toLowerCase();
			}
			if(g.startsWith(cstr)){
				if(orderedData){
				blockfound = true;
				}
				v.addElement(s);
			} else {
				if(blockfound){
					break;
				}
			}
		}
		String[] anArray = new String[v.size()];
		v.copyInto(anArray);
		return anArray;
	}

	public String[] contains(String str, int caseOptions) {
		Vector v = new Vector();
		String cstr = str;
		for(int i = 0 ; i < lenght; i++){
			String s = dictionary[i];
			String g = s;
			if(caseOptions == Diccionario.CASE_INSENSITIVE){
				g = s.toLowerCase();
				cstr = str.toLowerCase();
			}
			if(g.indexOf(cstr) > 0 ){
				v.addElement(dictionary[i]);
			}
		}
		String[] anArray = new String[v.size()];
		v.copyInto(anArray);
		return anArray;
	}
	
	public String[] endsWith(String suffix, int caseOptions) {
		Vector v = new Vector();
		String cstr = suffix;
		for(int i = 0 ; i < lenght; i++){
			String s = dictionary[i];
			String g = s;
			if(caseOptions == Diccionario.CASE_INSENSITIVE){
				g = s.toLowerCase();
				cstr = suffix.toLowerCase();
			}
			if(g.endsWith(cstr)){
				v.addElement(s);
			}
		}
		String[] anArray = new String[v.size()];
		v.copyInto(anArray);
		return anArray;
	}
	
	
}
